#!/usr/bin/python3
# coding: utf-8

import uapool
import zjl_util
import re
import os
import time
import requests
import getopt
import sys
import random


class TingChina:
    """ download voice from TingChina"""

    def __init__(self, u, d, offset):
        self.index_url = u
        self.dir = d
        self.start = offset
        if not os.path.exists(self.dir):
            os.makedirs(self.dir)
        uapool.UA()

    def get_file_info(self):
        """获取下载文件信息"""
        encoding_pat = 'charset=["]*(.*?)"'
        encoding = 'utf-8'
        
        resp = requests.get(self.index_url)
        encodes = re.compile(encoding_pat, re.S).findall(resp.text)
        if len(encodes) > 0:
            resp.encoding = encodes[0].replace(' ', '')
        else:
            resp.encoding = encoding
        doc = resp.text
        # print(doc)
        l_pat = '<div class="list">.*?</div>'
        r = re.compile(l_pat, re.S).findall(doc)
        # print(r[0:3])
        print("------------------------------------------filelist")

        a_pat = '<a href="(.*?)">(.*?)</a>'
        r = re.compile(a_pat, re.S).findall(r[0])
        # print(r[0:3])
        print(range(self.start, len(r)))
        for j in range(self.start, len(r)):
            print(j)
            random_num = random.random()
            i = r[j]
            file_name = i[1]
            item_url = i[0]
            r_index = str(self.index_url).rindex('/')
            item_url = self.index_url[0: r_index + 1] + item_url
            resp = requests.get(item_url)
            encodes = re.compile(encoding_pat, re.S).findall(resp.text)
            if len(encodes) > 0:
                resp.encoding = encodes[0].replace(' ', '')
            else:
                resp.encoding = encoding
            doc = resp.text
            mp3_pat = 'fileUrl= "(.*?)"'
            rt = re.compile(mp3_pat, re.S).findall(doc)
            file_url = 'https://t3344.tingchina.com' + rt[0]
            # 然后要获取访问的key
            key_url = 'https://img.tingchina.com/play/h5_jsonp.asp?' + str(random_num)
            headers = {
                'cookie': 'tingNewJieshaoren=0; ASPSESSIONIDAGRTDTAC=GAEBKFPBIHEKHGAAFNEHICEC; UM_distinctid=17964c28fcc26f-00b7c22213b974-5463377a-f0000-17964c28fd7143; tNew_play_url=' + item_url.replace(':', '%3A'),
                'referer': item_url
            }
            doc = requests.get(key_url, timeout=30, headers=headers).text
            key_pat = 'key=(.*?);'
            s = re.compile(key_pat, re.S).findall(doc)[0]
            key = s.split('"')
            key = key[len(key) - 2]
            
            file_url = file_url + '?key=' + key
            if len(key) < 10:
                print(doc)
            
            print('---------------(' + file_name + ', ' + file_url + ")---------------")
            self.down_mp3(file_url, file_name)
            # file_path = self.dir + "/" + file_name
            # if os.path.getsize(file_path) < 1000000:
            #     self.down_mp3(file_url, file_name)

    def down_mp3(self, file_url, file_name):
        """下载文件"""
        i = 0
        redo_count = 0  # 用于记录下载出现错误是记录重试次数，顺利下载完后，次数归0
        print('down: ' + file_url)
        while redo_count < 5:
            file_path = self.dir + "/" + file_name
            try:
                zjl_util.download(file_url, file_path)
                return
            except Exception as e:
                print("request error url:" + file_url)
                print(e)
                print('-------\t重试下载  ' + file_url + '  \t-------')
                redo_count += 1
                time.sleep(redo_count * 30 + 1) # 每次重试增加时间
                # 提前结束本次循环，再次下载这一章
                if redo_count >= 5:
                    print('[error] 多次重试后失败')
                    exit()


def main():
    index_url = "https://www.tingchina.com/yousheng/disp_30654.htm"
    dir_path = "D:/data/tingchina"
    offset = 0
    opts, args = getopt.getopt(sys.argv[1:], "hu:d:", ["dir=", "offset="])
    for o, a in opts:
        if o == '-h':
            print("python3 down_TingChina.py -u " + index_url + " -d " + dir_path + " --offset=" + str(offset))
            exit()
        if o == '-u':
            index_url = a
        if o == '-d':
            dir_path = a
        if o == '--offset':
            offset = int(a)
    print('目录地址：' + index_url)
    print('保存路径：' + dir_path)
    app = TingChina(index_url, dir_path, offset)
    app.get_file_info()


if __name__ == '__main__':
    main()
